package edu.nf.project.webs.accountsAndPermissions;

import edu.nf.project.entitys.entity.accountsAndPermissions.relevancy.Roles_Permissions;
import edu.nf.project.services.accountsAndPermissions.Roles_PermissionsService;
import edu.nf.project.services.exception.derivedAnomalies.RolesException;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/api/roles_permissions")
public class Roles_PermissionsController extends BaseController {
    @Autowired
    private Roles_PermissionsService roles_permissionsService;

    /**
     * 获取全部角色信息
     */
    @GetMapping
    public ResultVO<List<Roles_Permissions>> getAllRoles() {
        try {
            List<Roles_Permissions> roles = roles_permissionsService.getAll();
            return success(roles);
        } catch (Exception e) {
            throw new RolesException(20000, "获取角色列表失败");
        }
    }

    /**
     * 角色新增权限
     * @param rId 角色ID
     * @param pId 权限ID
     * @return 是否成功
     */
    @PostMapping("/add")
    public ResultVO<Boolean> addP(Integer rId, Integer pId) {
        try {
            Boolean flag = roles_permissionsService.addP(rId, pId);
            return success(flag);
        } catch (Exception e) {
            throw new RolesException(20000, "新增权限失败");
        }
    }

    /**
     * 角色删除权限
     * @param rId 角色ID
     * @param pId 权限ID
     * @return 是否成功
     */
    @PostMapping("/del")
    public ResultVO<Boolean> delP(Integer rId, Integer pId) {
        try {
            Boolean flag = roles_permissionsService.delP(rId, pId);
            return success(flag);
        } catch (Exception e) {
            throw new RolesException(20000, "删除权限失败");
        }
    }
}
